package com.easyboot.web.system.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyboot.utils.CommonUtil;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.framework.domain.KeyValueInt64Dto;
import com.easyboot.framework.shiro.UserOwnInfo;
import com.easyboot.web.system.domain.SysPosition;
import com.easyboot.web.system.domain.SysUserPosition;
import com.easyboot.web.system.mapper.SysPositionMapper;
import com.easyboot.web.system.mapper.SysUserPositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/25 14:40
 **/
@Service
public class SysPositionService {

    @Autowired
    private SysPositionMapper postionMapper;
    @Autowired
    private SysUserPositionMapper userPositionMapper;
    /**
     *
     * @author yushuo
     * @description //角色列表
     * @date 16:38 2021/2/1
     * @param [dto, roleName, roleCode]
     * @return com.easycms.framework.domain.DataTableDto
     **/
    public DataTableDto getList(DataTableDto dto, String postName, String postCode){
        LambdaQueryWrapper<SysPosition> query=new LambdaQueryWrapper<>();
        if(!StrUtil.isEmpty(postName)){
            query.like(SysPosition::getPostName,postName.trim());
        }
        if(!StrUtil.isEmpty(postCode)){
            query.like(SysPosition::getPostCode,postCode.trim());
        }
        query.orderByAsc(SysPosition::getPostSort);
        Page<SysPosition> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<SysPosition> userIPage = postionMapper.selectPage(page, query);
        dto.setTotal(userIPage.getTotal());
        dto.setRows(userIPage.getRecords());
        return dto;
    }

    /**
     *
     * @author yushuo
     * @description //角色列表
     * @date 16:38 2021/2/1
     * @param [dto, roleName, roleCode]
     * @return com.easycms.framework.domain.DataTableDto
     **/
    public List<KeyValueInt64Dto> getPositionSelectList(){
        LambdaQueryWrapper<SysPosition> query=new LambdaQueryWrapper<>();
        query.select(SysPosition::getId,SysPosition::getPostName)
            .orderByAsc(SysPosition::getPostSort);

        List<SysPosition> positionList = postionMapper.selectList(query);

        List<KeyValueInt64Dto> data=new ArrayList<>();
        for (SysPosition position:positionList){
            KeyValueInt64Dto dto=new KeyValueInt64Dto();
            dto.setId(position.getId());
            dto.setName(position.getPostName());
            data.add(dto);
        }
        return data;
    }
    /**
    *
     * @author yushuo
     * @description 获取职位信息
     * @date 14:43 2021/2/25
     * @param [id]
     * @return com.easycms.web.system.domain.SysPostion
    **/
    public SysPosition getModel(Long id){
        return postionMapper.selectById(id);
    }


    /**
    *
     * @author yushuo
     * @description 删除数据
     * @date 15:19 2021/2/25
     * @param [postion, userOwnInfo]
     * @return void
    **/
    public void saveModel(SysPosition postion, UserOwnInfo userOwnInfo){
        if(StrUtil.isEmpty(postion.getPostCode())){
            postion.setPostCode(postion.getPostName());
        }
        postion.setUpdateTime(DateUtil.date());
        postion.setUpdateBy(userOwnInfo.getLoginName());
        if(postion.getId()==0){
            postion.setCreateTime(DateUtil.date());
            postion.setCreateBy(userOwnInfo.getLoginName());

            postionMapper.insert(postion);
            return;
        }

        SysPosition oldPostion=postionMapper.selectById(postion.getId());
        if(oldPostion!=null){
            postion.setCreateBy(oldPostion.getCreateBy());
            postion.setCreateTime(oldPostion.getCreateTime());
            postionMapper.updateById(postion);
        }
    }


    /**
    *
     * @author yushuo
     * @description 删除数据
     * @date 15:15 2021/2/25
     * @param [ids]
     * @return void
    **/
    public void removeAll(String ids){
        List<Long> idsList= CommonUtil.toLongList(ids);

        postionMapper.deleteBatchIds(idsList);
    }


    /**
    *
     * @author yushuo
     * @description 获取用户职务信息
     * @date 16:38 2021/2/27
     * @param [userId]
     * @return java.lang.String
    **/
    public String getUserPositonsName(Long userId){
        LambdaQueryWrapper<SysUserPosition> userPLambda= Wrappers.lambdaQuery();
        userPLambda.eq(SysUserPosition::getUserId,userId)
                .select(SysUserPosition::getPositionId);
        List<Long> userPositions=userPositionMapper.selectList(userPLambda).stream().map(SysUserPosition::getPositionId).collect(Collectors.toList());

        if(userPositions.size()>0){
            LambdaQueryWrapper<SysPosition> postLambda= Wrappers.lambdaQuery();
            postLambda.in(SysPosition::getId,userPositions)
                    .select(SysPosition::getPostName);
            List<String> postNames=postionMapper.selectList(postLambda)
                    .stream().map(SysPosition::getPostName).collect(Collectors.toList());
            if(postNames.size()>0){
                return String.join(",",postNames);
            }
            return "暂无职务";
        }

        return "暂无职务";
    }
}
